﻿using CCSSolderUI.Common.Domain;
using Microsoft.EntityFrameworkCore;

namespace CCSSolderUI
{
    public class DTContext : DbContext
    {
        public string DbPath { get; }
        public DbSet<XYZPoint> XYZPoints { get; set; }
        public DbSet<WorkPoint> WorkPoints { get; set; }
        public DbSet<BarcodePoint> BarcodePoints { get; set; }
        public DTContext()
        {
            var path = System.AppDomain.CurrentDomain.BaseDirectory;
            DbPath = System.IO.Path.Join(path, "point.db");
        }
        public DTContext(string partNumber)
        {
            var path = System.AppDomain.CurrentDomain.BaseDirectory;
            DbPath = System.IO.Path.Join(path, partNumber, "point.db");
        }
        protected override void OnConfiguring(DbContextOptionsBuilder options)
        {
            options.UseSqlite($"Data Source={DbPath};Pooling=False");
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<XYZPoint>(entity =>
            {
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Name).HasMaxLength(100);
                entity.Property(e => e.Remark).HasMaxLength(1000).IsRequired(false);
            });
            modelBuilder.Entity<WorkPoint>(entity => 
            {
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Name).HasMaxLength(100);
                entity.Property(e => e.Remark).HasMaxLength(1000).IsRequired(false);
            });
            modelBuilder.Seed();
        }
    }
}
